var fcdImg = {};

(function(i){
    i.init = function(){
        var top,left,showedPic=0;
        
        $('#c-img').mousemove(function(){
            $('.img-toolbar').css('display', 'block');
        });

        $('#c-img').mouseout(function(){
            $('.img-toolbar').css('display', 'none');
        });

        $(".next").click(function(){
            window.location.href = p;
        });

        $(".zoom").click(function(){
             var showLimg = function(){
                $("#zoom-pic").css('left', left);
                $("#zoom-pic").css('top', top);
                $("#zoom-mask").css('height', $(document).height()+'px');
                $("#zoom-img").css('display', 'block');
                showedPic = 1;
            }

            if(showedPic == 0){
                $("#zoom-pic").attr('src', u);
                $("#zoom-pic").load(function(){
                    left = Math.ceil(($(document).width() - $("#zoom-pic").attr('width')) / 2);
                    top = 150;
                    showLimg();
                });
            }
            else{
                showLimg();
            }
        });

        $("#zoom-img").click(function(){
            $("#zoom-img").css('display', 'none');
        });

        $(".pre").click(function(){
            window.location.href = n;
        });
        $("span[class='like']").click(function(){
           $.get($(this).attr('value').replace("/f/", '/ul/'), {'t': (new Date()).getTime()}, function(data){
               var d = $.parseJSON(data);
               data = null;
                switch(d[0]){
                    case 'E_UPDATE_SUCCESSED':
                        alert('喜欢她，就爱她！');
                        var liked = $("span[class='info']").find("span").last();
                        liked.text(d[1]);
                    break;
                    case 'E_LIKED':
                        alert('距离产生美，点击一次够。');
                    break;
                    case 'E_IMG_NOT_EXISTS':
                        alert('图片未找到！');
                    break;
                    default:
                        alert('你做了什么？我不想理你了。');
                }
           });
        });

        $("#comms_ver").focusin(function(){
            $("#verification_code_text").trigger('click');
        });

        $("#verification_code_text").click(function(){
            var append = '?' + new Date().getTime() + 'a' + Math.random();
            $("#vc_input").html('<img src="/vc/'+append+'" border="0" id="verification_code"/>');
        });

        $("span[class='down']").click(function(){
            /*var iframe = $("<iframe id='download_pic_frame' style='display:none;' src='"+$(this).attr('value')+"'></iframe>");
            $(this).append(iframe);*/
            window.open($(this).attr('value'), 'pic_down', 'toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
        });

            $("#comform").validate({
                        rules: {
                            un: {
                                required: true,
                                minlength: 2
                            },
                            em: {
                                required: true,
                                email: true
                            },
                            url: {
                                required: false,
                                url: true
                            },
                            cmsa: {
                                required: true,
                                minlength: 10,
                                maxlength: 200
                            },
                            comms_ver: {
                                required: true,
                                minlength: 4,
                                maxlength: 4
                            }
                        },
                        messages: {
                            un: {
                                required: "请输入用户名",
                                minlength: "用户名必须大于2个字节"
                            },
                            cmsa: {
                                required: "请输入评论内容",
                                minlength: "评论内容必须大于10个字节",
                                maxlength: "评论内容必须小于200个字节"
                            },
                            em: {
                                required: "请输入电子邮件地址",
                                email: "请输入一个正确的电子邮件地址"
                            },
                            url: {
                                required: "请输入网站地址",
                                url: "请输入一个正确的网站地址"
                            },
                            comms_ver: {
                                required: "请输入验证码",
                                minlength: "验证码必须大于4个字节",
                                maxlength: "验证码必须小于4个字节"
                            }
                        },
                        submitHandler: function(form) {
                            var name = $("#un").val();
                            var email = $("#em").val();
                            var url = $("#url").val();
                            var contents = $("#cmsa").val();
                            var vercode = $("#comms_ver").val();
                            $.ajax({
                                url: '/ac/',
                                data: 'n='+name+'&e='+email+'&u='+url+'&c='+contents+'&v='+vercode+'&i='+u.replace("/f/",'').replace("/",''),
                                success: function(data){
                                    if (data.status == true)
                                    {
                                        alert('评论已经更新成功!');
                                        i.resetform();
                                        $("#verification_code_text").trigger('click');
                                    }
                                    else{
                                        if(data.msg == "VERCODE_NOT_EQ"){
                                            alert("验证码失效，请重输验证码!");
                                            $("#comms_ver").val('');
                                            $("#comms_ver").focus();
                                            $("#verification_code_text").trigger('click');
                                        }
                                        else{
                                            alert('评论失败了，请重输验证码!');
                                            $("#comms_ver").val('');
                                            $("#comms_ver").focus();
                                            $("#verification_code_text").trigger('click');
                                        }
                                    }
                                },
                                type: 'POST',
                                dataType: 'json'
                            });
                            return ;
                        }
                    });
    };
    i.resetform = function(){
        $("#un").val('');
        $("#em").val('');
        $("#url").val('');
        $("#cmsa").val('');
        $("#comms_ver").val('');
    };
    i.showComments = function(p){
        if(typeof(p) == "undefined"){
            p = 1;
        }
        $.get(u.replace("/f/",'/cl/')+'?p='+p, {},function(data){
            if(data.n>0){
                var cml = '';
                var d = data.d;
                for(var i = 0;i<data.n;i++){
                    cml += '<div class="comms"><div class="comms-avater"><img src="http://www.gravatar.com/avatar/'+d[i].av+'" border="0"/></div><div class="comms-comments"><a href="'+d[i].ur+'">'+d[i].un+'</a> - '+d[i].ct+'<br/><span style="color:#CCC">'+d[i].tt+'</span></div><div class="clear"></div></div>';
                }
                if(data.tp>1){
                    var np = p + 1;
                    var pp = p - 1;
                    if(data.tp > p && p > 1){
                        cml += '<div class="comms-link"><span onclick="javascript:fcdImg.showComments('+pp+');">上一页</span>&nbsp;<span onclick="javascript:fcdImg.showComments('+np+');">下一页</span></div>';
                    }
                    else{
                        if(p == 1){
                            cml += '<div class="comms-link"><span onclick="javascript:fcdImg.showComments('+np+');">下一页</span></div>';
                        }
                        else{
                            cml += '<div class="comms-link"><span onclick="javascript:fcdImg.showComments('+pp+');">上一页</span></div>';
                        }
                    }
                }
                $("#comms-div").html(cml);
            }
        }, 'json');
    };
})(fcdImg);

$(document).ready(function(){
    fcdImg.init();
    fcdImg.showComments();
});